package com.findtech.threePomelos.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.findtech.threePomelos.R;
import com.findtech.threePomelos.utils.RequestUtils;

import java.util.ArrayList;

/**
 * Created by zhi.zhang on 3/19/16.
 */
public class MyDeviceAdapter extends RecyclerView.Adapter<MyDeviceAdapter.MyViewHolder> {

    RequestUtils.MyItemClickListener mItemClickListener;
    Context mContext;
    boolean mIsBind = false;


    public MyDeviceAdapter(Context context) {
        mContext = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyDeviceAdapter.MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).
                inflate(R.layout.app_device_list_item, parent, false));//不添加false，会出现crash
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (mIsBind) {
            holder.blueToothBind.setText("已绑定");
            holder.blueToothBind.setTextColor(mContext.getResources().getColor(R.color.text_color_btn));
            holder.carImg.setBackground(mContext.getResources().getDrawable(R.mipmap.myfacility_icon_nor));
        } else {
            holder.blueToothBind.setText("未绑定");
            holder.blueToothBind.setTextColor(mContext.getResources().getColor(R.color.light_red_color));
            holder.carImg.setBackground(mContext.getResources().getDrawable(R.mipmap.myfacility_icon_dis));
        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public void setOnItemClickListener(RequestUtils.MyItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public void setIsBind(boolean isBind) {
        mIsBind = isBind;
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView carImg;
        TextView blueToothBind;

        public MyViewHolder(View itemView) {
            super(itemView);
            blueToothBind = (TextView) itemView.findViewById(R.id.blueToothBind);
            carImg = (ImageView) itemView.findViewById(R.id.car_img);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mItemClickListener != null) {
                        mItemClickListener.onItemClick(view, getPosition());
                    }
                }
            });
        }
    }
}
